import tkinter as tk
from user_info import UserInfo
from main_window import MainWindow
from my_menu import MyMenu
from tkinter import messagebox
from menu_type import MenuType
from user_session import UserSession

class LoginWindow:
    def __init__(self, root):
        # 创建登录窗口界面
        self.root = root
        root.title("用户登录")
        
        # 设置窗口大小和位置
        window_width = 400
        window_height = 300
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        x = (screen_width - window_width) // 2
        y = (screen_height - window_height) // 2
        root.geometry(f"{window_width}x{window_height}+{x}+{y}")
        
        # 设置窗口最小尺寸
        root.minsize(400, 300)
        
        # 设置窗口背景色
        root.configure(bg="#F0F0F0")
        
        # 使用Frame容器化管理组件
        iframe = tk.Frame(root, bg="#F0F0F0")
        iframe.place(relx=0.5, rely=0.5, anchor="center")
        
        # 系统标题
        sys_label = tk.Label(iframe, 
                           text="学生成绩管理系统", 
                           fg="#2E8B57",
                           bg="#F0F0F0",
                           font=("华文行楷", 24, "bold"))
        sys_label.grid(row=0, column=0, columnspan=2, pady=20)
        
        # 用户名输入
        name_label = tk.Label(iframe, 
                            text="用户名：", 
                            font=("微软雅黑", 12),
                            bg="#F0F0F0")
        self.name_entry = tk.Entry(iframe, 
                                 font=("微软雅黑", 12),
                                 width=20,
                                 relief="solid",
                                 borderwidth=1)
        name_label.grid(row=1, column=0, pady=10, padx=10)
        self.name_entry.grid(row=1, column=1, pady=10, padx=10)
        
        # 密码的输入
        pwd_label = tk.Label(iframe, 
                           text="密码：", 
                           font=("微软雅黑", 12),
                           bg="#F0F0F0")
        self.pwd_entry = tk.Entry(iframe, 
                                show="*",
                                font=("微软雅黑", 12),
                                width=20,
                                relief="solid",
                                borderwidth=1)
        pwd_label.grid(row=2, column=0, pady=10, padx=10)
        self.pwd_entry.grid(row=2, column=1, pady=10, padx=10)
        
        # 登录按钮
        login_button = tk.Button(iframe, 
                               text="登录", 
                               font=("微软雅黑", 12),
                               width=15,
                               bg="#2E8B57",
                               fg="white",
                               activebackground="#3CB371",  # 鼠标悬停时的颜色
                               activeforeground="white",
                               relief="raised",
                               borderwidth=2,
                               command=self.login)
        login_button.grid(row=3, column=0, columnspan=2, pady=20)
        
        # 绑定回车键到登录按钮
        self.pwd_entry.bind("<Return>", lambda event: self.login())
        self.name_entry.bind("<Return>", lambda event: self.pwd_entry.focus())
        
        # 配置网格权重，使组件居中
        iframe.grid_columnconfigure(0, weight=1)
        iframe.grid_columnconfigure(1, weight=1)
        
        # 使窗口可调整大小
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)
        
        # 设置窗口图标
        try:
            root.iconbitmap("icon.ico")  # 如果有图标文件的话
        except:
            pass
            
        # 设置窗口置顶
        root.attributes('-topmost', True)
        root.after(100, lambda: root.attributes('-topmost', False))
        
        # 自动聚焦到用户名输入框
        self.name_entry.focus()
        
        root.mainloop()

    def login(self):
        print("--------------")
        # 获取用户名和密码
        username = self.name_entry.get()
        password = self.pwd_entry.get()
        # 验证用户名和密码
        if self.validate_login(username,password):
            # 登录成功，弹出提示框
            tk.messagebox.showinfo("登录成功", "欢迎登录")
            # 销毁当前窗口，打开主界面
            self.root.destroy()
            # 创建主界面窗口
            main_window = tk.Tk()
            # 把登录账号存入会话中
            UserSession.set_attribute("login_name", username)
            # 查询用户信息
            user_info = UserInfo().query_by_loginname(username)
            # 把用户信息存入会话中
            UserSession.set_attribute(username, user_info)
            # 创建菜单
            MyMenu(main_window,sub_type=MenuType.MENU_HOME)
        else:
            # 登录失败，弹出提示框
            tk.messagebox.showerror("登录失败", "用户名或密码错误")
            
    def validate_login(self,username,pwd):
        #登录操作
        return UserInfo().login(username,pwd)


if __name__ == "__main__":
    # 创建主窗口
    root = tk.Tk()
    # 创建登录窗口
    login_window = LoginWindow(root)
    # 运行主循环
    root.mainloop()